package com.study.zzedu.ui.modifypwd;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.study.zzedu.R;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.base.BasePresenter;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.ui.login.LoginActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.SharedPreferencesHelper;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.utils.Utils;
import com.study.zzedu.widget.LoadingDialog;

import static com.study.zzedu.utils.Utils.rexCheckPassword;

public class ModifyPwdActivity extends BaseMvpActivity<BaseView, ModifyPwdPresenter> implements BaseView, View.OnClickListener {

    @Override
    protected int getLayoutID() {
        return R.layout.activity_pwd_modify;
    }

    @Override
    protected ModifyPwdPresenter createPresenter() {
        return new ModifyPwdPresenter();
    }


    @Override
    protected BaseView createView() {
        return this;
    }

    private EditText etOld,etNew,etNewAgain;
    private Button btn_modify;
    private RelativeLayout btn_back;
    @Override
    protected void initView() {

        btn_back =  findViewById(R.id.rl_back);
        etOld = (EditText)findViewById(R.id.et_pwd_current);
        etNew = (EditText)findViewById(R.id.et_pwd);
        etNewAgain = (EditText)findViewById(R.id.et_pwd_again);
        btn_modify = (Button)findViewById(R.id.btn_modify);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        btn_modify.setOnClickListener(this);
        btn_back.setOnClickListener(this);
    }

    @Override
    public void getData(String data) {
        autoProgress(false);
        ToastUtil.showShortToast("密码修改成功");
        SharedPreferencesHelper.getInstance(ModifyPwdActivity.this).put("loginState", AppConstant.NOT_AUTO_LOGIN);

        ModifyPwdActivity.this.setResult(AppConstant.result_code_back_to_home);
        ModifyPwdActivity.this.finish();
    }

    @Override
    public void showError(String error) {
        autoProgress(false);
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_modify){
            if(TextUtils.isEmpty(etOld.getText().toString().trim()) || TextUtils.isEmpty(etNew.getText().toString().trim()) || TextUtils.isEmpty(etNewAgain.getText().toString().trim()))
            {
                ToastUtil.showShortToast("不能出现空字符");
                return;
            }
            if(!(etNew.getText().toString().trim().equals((etNewAgain.getText().toString().trim()))))
            {
                ToastUtil.showShortToast("两次输入不一样");
                return;
            }
            if(!rexCheckPassword(etNew.getText().toString().trim())){
                ToastUtil.showShortToast("大小写字母,数字,特殊字符中的4种.8位以上");
                return;
            }
            autoProgress(true);
            getPresenter().modify(ModifyPwdActivity.this, Utils.digest(etOld.getText().toString().trim()),Utils.digest(etNew.getText().toString().trim()));
        }else {
            finish();
        }
    }

    private LoadingDialog mDialog;
    private void autoProgress(boolean show){
        if(mDialog==null){
            mDialog = new LoadingDialog(ModifyPwdActivity.this);
            //mDialog.setMessage("正在加载数据。。。");
        }
        if(show){
            mDialog.show();
        }else{
            mDialog.dismiss();
        }
    }
}